package com.sharp.portal.controller;

import com.sharp.kernel.config.response.Response;
import com.sharp.kernel.config.response.ResponseHandler;
import com.sharp.kernel.vo.common.CommonResVO;
import com.sharp.kernel.vo.common.KeyValueResVO;
import com.sharp.portal.service.PortalManageService;
import com.sharp.portal.vo.ConditionVO;
import com.sharp.portal.vo.FieldDisplayVO;
import com.sharp.portal.vo.ModelDisplayVO;
import com.sharp.portal.vo.manage.ConditionUpdateVO;
import com.sharp.portal.vo.manage.FieldDisplayUpdateVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Title: PortalManagerController
 * Description: Copyright: Copyright (c) 2022 Company: Sharp Ltd.
 *
 * @author Sharp
 * @date 2022/09/30 10:51
 */

@RestController("PortalManagerController")
@RequestMapping(value = "/portal/manage")
public class PortalManagerController {

    @Resource
    private PortalManageService portalManageService;

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value = "获取所有可配置页面", notes = "获取所有可配置页面")
    public ResponseEntity<Response<List<KeyValueResVO>>> getPage() {
        List<KeyValueResVO> res = portalManageService.getAllPage();
        return ResponseHandler.generateResponse(res);
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    @ApiOperation(value = "获取指定页面基础信息", notes = "获取指定页面基础信息")
    public ResponseEntity<Response<ModelDisplayVO>> getModel(String uri) {
        ModelDisplayVO vo = portalManageService.getModelDisplay(uri);
        return ResponseHandler.generateResponse(vo);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    @ApiOperation(value = "编辑指定页面基础信息", notes = "编辑指定页面基础信息")
    public ResponseEntity<Response<CommonResVO>> updateModel(@RequestBody ModelDisplayVO vo) {
        int res = portalManageService.updateModelDisplay(vo);
        return ResponseHandler.commonResponse(res);
    }

    @RequestMapping(value = "/layout", method = RequestMethod.GET)
    @ApiOperation(value = "获取指定页面字段信息", notes = "获取指定页面字段信息")
    public ResponseEntity<Response<List<FieldDisplayVO>>> getLayout(String uri) {
        List<FieldDisplayVO> res = portalManageService.getLayout(uri);
        return ResponseHandler.generateResponse(res);
    }

    @RequestMapping(value = "/layout", method = RequestMethod.POST)
    @ApiOperation(value = "编辑指定页面字段信息", notes = "编辑指定页面字段信息")
    public ResponseEntity<Response<CommonResVO>> updateLayout(@RequestBody FieldDisplayUpdateVO vo) {
        int res = portalManageService.updateLayout(vo);
        return ResponseHandler.commonResponse(res);
    }

    @RequestMapping(value = "/condition", method = RequestMethod.GET)
    @ApiOperation(value = "查询指定页面查询条件", notes = "查询指定页面查询条件")
    public ResponseEntity<Response<List<ConditionVO>>> getCondition(String uri) {
        List<ConditionVO> res = portalManageService.getCondition(uri);
        return ResponseHandler.generateResponse(res);
    }

    @RequestMapping(value = "/condition", method = RequestMethod.POST)
    @ApiOperation(value = "编辑指定页面查询条件", notes = "编辑指定页面查询条件")
    public ResponseEntity<Response<CommonResVO>> updateCondition(@RequestBody ConditionUpdateVO vo) {
        int res = portalManageService.updateCondition(vo);
        return ResponseHandler.commonResponse(res);
    }

    @RequestMapping(value = "/refresh", method = RequestMethod.POST)
    @ApiOperation(value = "重新生成相关信息", notes = "重新生成相关信息")
    public ResponseEntity<Response<CommonResVO>> refresh(String uri) {
        portalManageService.refresh(uri);
        return ResponseHandler.commonResponse();
    }
}
